<template>
	<view class="container">
		<view class="group-box">
			<view class="form-uni-list">
				<uni-forms :value="model" :rules="rules" ref="form">
					<uni-forms-item name="password" class="list-box">
						<uni-easyinput type="password" v-model="model.password" placeholder="请输入旧密码" />
					</uni-forms-item>
					<uni-forms-item name="newPassword" class="list-box">
						<uni-easyinput type="password" v-model="model.newPassword" placeholder="请输入新密码" />
					</uni-forms-item>
					<uni-forms-item name="confirmPassword" class="list-box">
						<uni-easyinput type="password" v-model="model.confirmPassword" placeholder="请再次输入新密码" />
					</uni-forms-item>
					<view class="form-foot">
						<button @click="submitForm" class="btn submit">确认提交</button>
					</view>
				</uni-forms>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				model: {
					password: null,
					newPassword: null,
					confirmPassword: null
				},
				rules: {
					password: {
						rules: [
							{
								required: true,
								errorMessage: '请输入旧密码',
							},
							{
								pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
								errorMessage: '旧密码必须6-20位英文字母数字组合',
							}
						]
					},
					newPassword: {
						rules: [
							{
								required: true,
								errorMessage: '请输入新密码',
							},
							{
								pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
								errorMessage: '新密码必须6-20位英文字母数字组合',
							}
						]
					},
					confirmPassword: {
						rules: [
							{
								required: true,
								errorMessage: '请再次输入新密码',
							},
							{
								validateFunction:function(rule,value,data,callback){
									console.log(data)
									if (value !== data.newPassword) {
										callback('两次密码输入不一致')
									}
									return true
								},
							}
						]
					},
				}
			}
		},
		methods: {
			//提交表单
			submitForm() {
				let _this = this;
				_this.$refs.form.validate().then(async res=>{
					//console.log('表单数据信息：', res);
					await this.$api.request({
						url: `/account/member/password`,
						method: "put",
						data: _this.model,
						loading: true
					}).then(res=>{
						_this.$api.msg(`密码修改成功`,1500,false,'success');
						_this.$refs.form.resetFields();
					});
				}).catch(err =>{
					//console.log('表单错误信息：', err);
				});
			}
		}
	}
</script>
